home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / JFC.bin / JColorChooser.java < prev    next >
Text File  |  1998-06-30  |  12KB  |  367 lines

  1. /*
  2.  * @(#)JColorChooser.java    1.33 98/02/02
  3.  * 
  4.  * Copyright (c) 1997 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not disclose
  8.  * such Confidential Information and shall use it only in accordance with the
  9.  * terms of the license agreement you entered into with Sun.
  10.  * 
  11.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  12.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
  14.  * OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY
  15.  * LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR
  16.  * ITS DERIVATIVES.
  17.  * 
  18.  */
  19.  
  20. package com.sun.java.swing.preview;
  21.  
  22. import java.awt.*;
  23. import java.awt.event.*;
  24. import java.io.*;
  25. import java.beans.*;
  26.  
  27. import com.sun.java.swing.*;
  28. import com.sun.java.swing.plaf.ColorChooserUI;
  29. import com.sun.java.swing.event.*;
  30.  
  31. /**
  32.  * JColorChooser provides a pane of controls designed to allow 
  33.  * a user to manipulate and select a color.
  34.  *
  35.  * This class provides 3 levels of API:
  36.  * <ol>
  37.  * <li>A static convenience method which shows a modal color-chooser
  38.  * dialog and returns the color selected by the user.
  39.  * <li>A static convenience method for creating a color-chooser dialog
  40.  * where ActionListeners can be specified to be invoked when
  41.  * the user presses one of the dialog buttons.
  42.  * <li>The ability to create instances of JColorChooser panes
  43.  * directly (within any container). PropertyChange listeners
  44.  * can be added to detect when the current "color" property changes.
  45.  * </ol>
  46.  * <p>
  47.  * Warning: serialized objects of this class will not be compatible with
  48.  * future swing releases.  The current serialization support is appropriate 
  49.  * for short term storage or RMI between Swing1.0 applications.  It will
  50.  * not be possible to load serialized Swing1.0 objects with future releases
  51.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  52.  * baseline for the serialized form of Swing objects.
  53.  * <p>
  54.  * Warning: serialized objects of this class will not be compatible with
  55.  * future swing releases.  The current serialization support is appropriate 
  56.  * for short term storage or RMI between Swing1.0 applications.  It will
  57.  * not be possible to load serialized Swing1.0 objects with future releases
  58.  * of Swing.  The JDK1.2 release of Swing will be the compatibility
  59.  * baseline for the serialized form of Swing objects.
  60.  *
  61.  * @version 1.33 02/02/98
  62.  * @author James Gosling
  63.  * @author Amy Fowler
  64.  */
  65. public class JColorChooser extends JComponent implements Serializable {
  66.  
  67.     /**
  68.      * The color property name.
  69.      */
  70.     public static final String      COLOR_PROPERTY = "color";
  71.  
  72.     /**
  73.      * Shows a modal color-chooser dialog and blocks until the
  74.      * dialog is hidden.  If the user presses the "OK" button, then
  75.      * this method hides/disposes the dialog and returns the selected color.  
  76.      * If the user presses the "Cancel" button or closes the dialog without 
  77.      * pressing "OK", then this method hides/disposes the dialog and returns
  78.      * null.
  79.      *
  80.      * @param component    the parent Component for the dialog
  81.      * @param title        the String containing the dialog's title
  82.      * @param initialColor the initial Color set when the color-chooser is shown
  83.      */
  84.     public static Color showDialog(Component component, 
  85.                                    String title, Color initialColor) {
  86.                                        
  87.         final JColorChooser pane = new JColorChooser(initialColor != null? 
  88.                                                initialColor : Color.white);
  89.  
  90.         ColorTracker ok = new ColorTracker(pane);
  91.         JDialog dialog = createDialog(component, title, true, pane, ok, null);
  92.         dialog.addWindowListener(new ColorChooserDialog.Closer());
  93.         dialog.addComponentListener(new ColorChooserDialog.DisposeOnClose());
  94.  
  95.         dialog.show(); // blocks until user brings dialog down...
  96.  
  97.         return ok.getColor();
  98.     }
  99.  
  100.  
  101.     /**
  102.      * Creates and returns a new dialog containing the specified
  103.      * ColorChooser pane along with "OK", "Cancel", and "Reset" buttons. 
  104.      * If the "OK" or "Cancel" buttons are pressed, the dialog is
  105.      * automatically hidden (but not disposed).  If the "Reset" 
  106.      * button is pressed, the color-chooser's color will be reset to the
  107.      * color which was set the last time show() was invoked on the dialog
  108.      * and the dialog will remain showing.
  109.      *
  110.      * @param c              the parent component for the dialog
  111.      * @param title          the title for the dialog
  112.      * @param modal          a boolean. When true, the remainder of the program
  113.      *                       is inactive until the dialog is closed.
  114.      * @param chooserPane    the color-chooser to be placed inside the dialog
  115.      * @param okListener     the ActionListener invoked when "OK" is pressed
  116.      * @param cancelListener the ActionListener invoked when "Cancel" is pressed
  117.      */
  118.     public static JDialog createDialog(Component c, String title, boolean modal,
  119.                                        JColorChooser chooserPane, 
  120.                                        ActionListener okListener,
  121.                                        ActionListener cancelListener) {
  122.  
  123.         return new ColorChooserDialog(c, title, modal, chooserPane,
  124.                                                 okListener, cancelListener);
  125.     }
  126.  
  127.     private Color color;
  128.  
  129.     /**
  130.      * Creates a color chooser pane with an initial color of white.
  131.      */
  132.     public JColorChooser() {
  133.         this(Color.white);
  134.     }
  135.  
  136.     /**
  137.      * Creates a color chooser pane with the specified initial color.
  138.      *
  139.      * @param initialColor the initial color set in the chooser
  140.      */
  141.     public JColorChooser(Color initialColor) {
  142.         updateUI();
  143.         setColor(initialColor);
  144.     }
  145.  
  146.     /**
  147.      * Returns the L&F object that renders this component.
  148.      *
  149.      * @return the ColorChooserUI object that renders this component
  150.      */
  151.     public ColorChooserUI getUI() {
  152.         return (ColorChooserUI)ui;
  153.     }
  154.  
  155.     /**
  156.      * Sets the L&F object that renders this component.
  157.      *
  158.      * @param ui  the ColorChooserUI L&F object
  159.      * @see UIDefaults#getUI
  160.      */
  161.     public void setUI(ColorChooserUI ui) {
  162.         super.setUI(ui);
  163.     }
  164.  
  165.     /**
  166.      * Notification from the UIManager that the L&F has changed. 
  167.      * Replaces the current UI object with the latest version from the 
  168.      * UIManager.
  169.      *
  170.      * @see JComponent#updateUI
  171.      */
  172.     public void updateUI() {
  173.         setUI((ColorChooserUI)UIManager.getUI(this));
  174.     }
  175.  
  176.     /**
  177.      * Returns the name of the L&F class that renders this component.
  178.      *
  179.      * @return "ColorChooserUI"
  180.      * @see JComponent#getUIClassID
  181.      * @see UIDefaults#getUI
  182.      */
  183.     public String getUIClassID() {
  184.         return "ColorChooserUI";
  185.     }
  186.  
  187.     /** 
  188.      * Gets the current color value from the color chooser.
  189.      * By default, this delegates to the model.
  190.      *
  191.      * @return the current color value of the color chooser
  192.      */
  193.     public Color getColor() {
  194.         return color;
  195.     }
  196.  
  197.     /**
  198.      * Sets the current color of the color chooser to the
  199.      * specified color.
  200.      * This will fire a PropertyChangeEvent for the property
  201.      * named "color".
  202.      *
  203.      * @param color the color to be set in the color chooser
  204.      * @see JComponent#addPropertyChangeListener
  205.      */
  206.     public void setColor(Color color) {
  207.         if (!color.equals(this.color)) {
  208.             Color old = this.color;
  209.             this.color = color;
  210.             firePropertyChange(JColorChooser.COLOR_PROPERTY, old, color);
  211.         }
  212.     }
  213.  
  214.     /**
  215.      * Sets the current color of the color chooser to the
  216.      * specified RGB color.
  217.      *
  218.      * @param r   an int specifying the amount of Red
  219.      * @param g   an int specifying the amount of Green
  220.      * @param b   an int specifying the amount of Blue
  221.      */
  222.     public void setColor(int r, int g, int b) {
  223.         setColor(new Color(r,g,b));     
  224.     }
  225.  
  226.     /**
  227.      * Sets the current color of the color chooser to the
  228.      * specified color.
  229.      *
  230.      * @param c an int value that sets the current color in the chooser
  231.      *          where the low-order 8 bits specify the Blue value,
  232.      *          the next 8 bits specify the Green value, and the 8 bits
  233.      *          above that specify the Red value.
  234.      */
  235.     public void setColor(int c) {
  236.         setColor((c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF);
  237.     }
  238.  
  239.     /**
  240.      * Adds a color chooser panel to the color chooser.
  241.      *
  242.      * @param name   a string that specifies the name of the panel
  243.      */
  244.     public void addChooserPanel( String name, ColorChooserPanel panel ) {
  245.         getUI().addChooserPanel( name, panel );
  246.     }
  247.  
  248.     /**
  249.      * Removes the ColorChooserPanel specified by "name".
  250.      *
  251.      * @param name   a string that specifies the panel to be removed
  252.      */
  253.     public ColorChooserPanel removeChooserPanel( String name ) {
  254.         return getUI().removeChooserPanel( name );
  255.     }
  256. }
  257.  
  258. /*
  259.  * Class which builds a color chooser dialog consisting of
  260.  * a JColorChooser with "Ok", "Cancel", and "Reset" buttons.
  261.  *
  262.  * Note: This needs to be fixed to deal with localization!
  263.  */
  264. class ColorChooserDialog extends JDialog {
  265.     private Color initialColor;
  266.     private JColorChooser chooserPane;
  267.  
  268.     public ColorChooserDialog(Component c, String title, boolean modal,
  269.                               JColorChooser chooserPane,
  270.                               ActionListener okListener, ActionListener cancelListener) {
  271.         super(JOptionPane.getFrameForComponent(c), title, modal);
  272.         //setResizable(false);
  273.  
  274.         this.chooserPane = chooserPane;
  275.  
  276.         Container contentPane = getContentPane();        
  277.         contentPane.setLayout(new BorderLayout());
  278.         contentPane.add(chooserPane, BorderLayout.CENTER);
  279.  
  280.         /*
  281.          * Create Lower button panel
  282.          */
  283.         JPanel buttonPane = new JPanel();
  284.         buttonPane.setLayout(new FlowLayout(FlowLayout.CENTER));
  285.         JButton okButton = new JButton("OK");
  286.         okButton.setActionCommand("OK");
  287.         if (okListener != null) {
  288.             okButton.addActionListener(okListener);
  289.         }
  290.         okButton.addActionListener(new ActionListener() {
  291.             public void actionPerformed(ActionEvent e) {
  292.                 hide();
  293.             }
  294.         });
  295.         buttonPane.add(okButton);
  296.  
  297.         JButton cancelButton = new JButton("Cancel");
  298.         cancelButton.setActionCommand("cancel");
  299.         if (cancelListener != null) {
  300.             cancelButton.addActionListener(cancelListener);
  301.         }
  302.         cancelButton.addActionListener(new ActionListener() {
  303.             public void actionPerformed(ActionEvent e) {
  304.                 hide();
  305.             }
  306.         });
  307.         buttonPane.add(cancelButton);
  308.  
  309.         JButton resetButton = new JButton("Reset");
  310.         resetButton.addActionListener(new ActionListener() {
  311.            public void actionPerformed(ActionEvent e) {               
  312.                reset();
  313.            }
  314.         });
  315.         buttonPane.add(resetButton);
  316.         contentPane.add(buttonPane, BorderLayout.SOUTH);
  317.  
  318.         pack();
  319.         setLocationRelativeTo(c);
  320.  
  321.     }
  322.  
  323.     public void show() {
  324.         initialColor = chooserPane.getColor();
  325.         super.show();
  326.     }
  327.  
  328.     public void reset() {
  329.         chooserPane.setColor(initialColor);
  330.     }
  331.             
  332.     static class Closer extends WindowAdapter implements Serializable{        
  333.         public void windowClosing(WindowEvent e) {
  334.             Window w = e.getWindow();
  335.             w.hide();
  336.         }
  337.     }
  338.  
  339.     static class DisposeOnClose extends ComponentAdapter implements Serializable{
  340.         public void componentHidden(ComponentEvent e) {
  341.             Window w = (Window)e.getComponent();
  342.             w.dispose();
  343.         }
  344.     }
  345.  
  346. }
  347.  
  348. class ColorTracker implements ActionListener, Serializable {
  349.     JColorChooser chooser;
  350.     Color color;
  351.  
  352.     public ColorTracker(JColorChooser c) {
  353.         chooser = c;
  354.     }
  355.  
  356.     public void actionPerformed(ActionEvent e) {
  357.         color = chooser.getColor();
  358.     }
  359.  
  360.     public Color getColor() {
  361.         return color;
  362.     }
  363. }
  364.  
  365.  
  366.  
  367.